package miniSchedule;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

public class Job implements Comparable<Job>, Runnable{
    private final long id;
    private final Runnable originalTask;
    private long startTime;
    private final long delay;
    private final boolean isPeriodic;

    private volatile boolean cancelled = false;
    private volatile Thread runningThread; // 记录执行本任务的线程

    // 构造函数
    public Job(long id, Runnable task, long startTime, long delay, boolean isPeriodic) {
        this.id = id;
        this.originalTask = task;
        this.startTime = startTime;
        this.delay = delay;
        this.isPeriodic = isPeriodic;
    }

    @Override
    public void run() {
        if (cancelled) {
            return;
        }
        runningThread = Thread.currentThread();
        try {
            originalTask.run();
        } finally {
            runningThread = null;
        }
    }

    public boolean isCancelled() { return cancelled; }
    public void cancel() { this.cancelled = true; }
    public long getId() { return id; }
    public Runnable getOriginalTask() { return originalTask; }
    public long getStartTime() { return startTime; }
    public void setStartTime(long startTime) { this.startTime = startTime; }
    public long getDelay() { return delay; }
    public boolean isPeriodic() { return isPeriodic; }

    @Override
    public int compareTo(Job o) {
        return Long.compare(this.startTime, o.startTime);
    }
}
